--- 模块功能：新设备发送HTTP请求获取clientNo.
-- @author zhangfenghe
-- @module Tweeter.getNewClientNo
-- @license MIT
-- @copyright openLuat
-- @release 2022.04.26
module(..., package.seeall)

require "http"
require "nvm"
require "registerTask"
require "mqttTask"
require "mqttTask61713"
require "mqttTaskQuakeInfoRel"
require "rtos"
require "logGenerator"

-- 初始化nvm获取配置参数
nvm.init("config.lua", true)
local clientNo, logInfo, errInfo, sysTime, regCenter, clientId =
    nvm.get("info")[1], "", "", "", nvm.get("regCenter"),
    nvm.get("centerConfig")[6]
-- 外接mqtt服务器连接任务
local function mqttTaskQuakeInfoRelAsk()
    mqttTaskQuakeInfoRel.startMQTTClient(clientNo)
end
-- 外接mqtt61713服务器连接任务
local function mqtt61713TaskAsk()
    mqttTask61713.startMQTTClient(clientNo)
end
-- 本司mqtt服务器连接任务
local function mqttTaskAsk() mqttTask.startMQTTClient(clientNo) end
local function cbFnc(result, prompt, head, body)
    log.info("getNewClientNo.cbFnc", result, prompt)
    if result and head then
        for k, v in pairs(head) do
            log.info("getNewClientNo.cbFnc", k .. ": " .. v)
            logInfo = logInfo .. "[" .. sysTime .. "]" ..
                          "[getNewClientNo.cbFnc:]" .. k .. ": " .. v .. "\n"
        end
    else
        errInfo = errInfo .. "[" .. sysTime .. "]" ..
                      "[获取新设备号请求头错误！]\n"
    end
    if result and body then
        log.info("getNewClientNo.cbFnc", "bodyLen=" .. body:len())
        log.info("body", body)
        clientNo = body
        for i = 1, 10 do log.info("[clientNo:]" .. clientNo .. "\n") end
        logInfo =
            logInfo .. "[" .. sysTime .. "]" .. "[getNewClientNo.cbFnc:]" ..
                "bodyLen=" .. body:len() .. ",body=" .. body .. "\n"
        nvm.sett("info", 1, clientNo, "SVR")
        sys.taskInit(mqttTaskAsk)
        sys.taskInit(mqtt61713TaskAsk)
        if regCenter then sys.taskInit(mqttTaskQuakeInfoRelAsk) end
    else
        errInfo = errInfo .. "[" .. sysTime .. "]" ..
                      "[获取新设备号请求体错误！]\n"
        logGenerator.generateLogTask(errInfo, "log")
    end
    log.info("getAndSetClientNo")
end
-- 获取并设置设备号
local function getAndSetClientNo()
    if "true" == regCenter then
        regCenter = true
    else
        regCenter = false
    end
    logInfo = ""
    sysTime = string.format("%04d-%02d-%02d %02d:%02d:%02d", os.date("*t").year,
                            os.date("*t").month, os.date("*t").day,
                            os.date("*t").hour, os.date("*t").min,
                            os.date("*t").sec)
    if clientNo == "KS000807" or clientNo == nil then
        http.request("GET", "http://47.94.80.3:8080/quake/register?clientNo=",
                     nil, nil, nil, nil, cbFnc)
    else
        sys.taskInit(mqttTaskAsk)
        sys.taskInit(mqtt61713TaskAsk)
        log.info("regCenterNewNo", regCenter, "clientId", clientId)
        if regCenter then
            if clientId == "" or clientId == nil then
                log.info("zi动注册")
                registerTask.RegistrationServiceTask()
            end
            sys.taskInit(mqttTaskQuakeInfoRelAsk)
        end
    end
    logGenerator.generateLogTask(logInfo, "log")
end

getAndSetClientNo()
